package com.huobao.yunying.fragments;

import java.util.ArrayList;
import java.util.List;

import org.simple.eventbus.EventBus;

import com.huobao.yunying.activitys.muyingquan.Act_Muyingquan;
import com.huobao.yunying.activitys.muyingquan.Act_My_Shequ;
import com.huobao.yunying.activitys.muyingquan.Act_Quanzi_Detail;
import com.huobao.yunying.activitys.muyingquan.Act_Quanzi_Fatie;
import com.huobao.yunying.adapters.ViewPagerAdapter;
import com.huobao.zhangying.R;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Fragment_MuyingQuan extends Fragment implements OnCheckedChangeListener,OnPageChangeListener,OnClickListener{

	private RadioGroup rg;
	private ViewPager viewPager;
	private ViewPagerAdapter adapter;
	private List<View> views;
	private LocalActivityManager manager = null;  
	private RadioButton rb_1,rb_2;
	private ImageView btn_back;
	private ImageView fatie;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_muyingquan, null);
		initView(view);
		return view;
	}
	private void initView(View view) {
		fatie = (ImageView) view.findViewById(R.id.fatie);
		btn_back = (ImageView) view.findViewById(R.id.btn_back);
		rb_1 = (RadioButton) view.findViewById(R.id.rb_1);
		rb_2 = (RadioButton) view.findViewById(R.id.rb_2);
		rg = (RadioGroup) view.findViewById(R.id.radiogroup);
		viewPager = (ViewPager) view.findViewById(R.id.view_1);
		rg.setOnCheckedChangeListener(this);
		viewPager.setOnPageChangeListener(this);
		btn_back.setOnClickListener(this);
		fatie.setOnClickListener(this);
		
	}
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		EventBus.getDefault().post(isVisibleToUser, "setUserVisibleHint");
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		manager = new LocalActivityManager(getActivity(), true);  
        manager.dispatchCreate(savedInstanceState);  
        
        views = new ArrayList<View>();
        Intent i1 = new Intent(getActivity(), Act_Muyingquan.class);  
        views.add(getView("A", i1));  
        Intent i2 = new Intent(getActivity(), Act_My_Shequ.class);  
        views.add(getView("B", i2));  
        adapter = new ViewPagerAdapter(views);   
		viewPager.setAdapter(adapter);  
	}
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId){
		case R.id.rb_1:
			viewPager.setCurrentItem(0);
			break;
		case R.id.rb_2:
			viewPager.setCurrentItem(1);
			break;
		}
	}
	
    private View getView(String id, Intent intent) {  
        return manager.startActivity(id, intent).getDecorView();  
    }
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}
	@Override
	public void onPageSelected(int arg0) {
		switch(arg0){
		case 0:
			rb_1.setChecked(true);
			break;
		case 1:
			rb_2.setChecked(true);
			break;
		}
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_back:
			EventBus.getDefault().post("", "goFristPage");
			break;
		case R.id.fatie:
			Intent intent=new Intent(getActivity(),Act_Quanzi_Fatie.class);
            startActivity(intent); 
			break;
		}
	}  
}
